﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Microsoft.Phone.Data.Linq;
using Microsoft.Phone.Data.Linq.Mapping;
using MapHello.Basic;
using System.IO;


namespace MapHello.Data
{
    [Table]
    [Index(Columns = "Name")]
    [Index(Columns = "Id")]
    [Index(Columns = "IsMe")]
    public class Person : DataEntity
    {
        public Person()
        {

        }



        private string _id;
        [Column(IsPrimaryKey = true, DbType = "NVARCHAR(50) NOT NULL", CanBeNull = false)]
        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _name;
        [Column(DbType = "NVARCHAR(50) NOT NULL")]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                NotifyPropertyChanging("Name");
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private string _imageUrl;
        [Column(DbType = "NVARCHAR(1024)", CanBeNull = true)]
        public string ImageUrl
        {
            get
            {
                return _imageUrl;
            }
            set
            {
                NotifyPropertyChanging("ImageUrl");
                _imageUrl = value;
                NotifyPropertyChanged("ImageUrl");
            }
        }

        private byte[] _imageBinary;
        [Column(DbType = "IMAGE", CanBeNull = true, UpdateCheck= UpdateCheck.Never)]
        public byte[] ImageBinary
        {
            get
            {
                if (null == _imageBinary)
                {
                    TryLoadingImageAsync();
                }
                return _imageBinary;
            }
            internal set
            {
                NotifyPropertyChanging("ImageBinary");
                _imageBinary = value;
                NotifyPropertyChanged("ImageBinary");
            }
        }

        private string _type = "Live";
        [Column(DbType = "NVARCHAR(20) NOT NULL")]
        public string Type
        {
            get
            {
                return _type;
            }
            set
            {
                NotifyPropertyChanging("Type");
                _type = value;
                NotifyPropertyChanged("Type");
            }
        }

        public Position Position
        {
            get
            {
                return new Position()
                {
                    Latitude = this._latitude,
                    Longitude = this._longitude,
                    ModifiedOn = this._positionModifiedOn
                };
            }
            set
            {
                NotifyPropertyChanging("Position");
                this.Latitude = value.Latitude;
                this.Longitude = value.Longitude;
                this.PositionModifiedOn = value.ModifiedOn;
                NotifyPropertyChanged("Position");
            }
        }

        private Nullable<double> _latitude;
        [Column(DbType = "FLOAT", CanBeNull = true)]
        private Nullable<double> Latitude
        {
            get
            {
                return _latitude;
            }
            set
            {
                _latitude = value;
                NotifyPropertyChanged("Latitude");
            }
        }

        private Nullable<double> _longitude;
        [Column(DbType = "FLOAT", CanBeNull = true)]
        private Nullable<double> Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                _longitude = value;
                NotifyPropertyChanged("Longitude");
            }
        }

        private Nullable<DateTime> _positionModifiedOn;
        [Column(DbType = "DATETIME", CanBeNull = true)]
        private Nullable<DateTime> PositionModifiedOn
        {
            get
            {
                return _positionModifiedOn;
            }
            set
            {
                _positionModifiedOn = value;
                NotifyPropertyChanged("PositionModifiedOn");
            }
        }



        private bool _isMe;
        [Column(DbType = "BIT", CanBeNull = false)]
        public bool IsMe
        {
            get
            {
                return _isMe;
            }
            set
            {
                NotifyPropertyChanging("Position");
                _isMe = value;
                NotifyPropertyChanged("Position");
            }
        }

        

        private async void TryLoadingImageAsync()
        {
            var result = await Utility.LoadImage(this._imageUrl);

            this._imageUrl = result.Key;
            this.ImageBinary = result.Value;
        }

    }
}
